package cn.south.campus.business.controller;

import cn.south.campus.business.model.vo.AddressVo;
import cn.south.campus.business.service.AddressService;
import cn.south.campus.common.model.bo.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author chow
 * @since 2024/11/14 15:39
 */
@RestController
@AllArgsConstructor
@RequestMapping("/campus/address")
public class AddressController {

    private final AddressService addressService;

    @GetMapping("/list")
    public Result<List<AddressVo>> list() {
        return Result.success(addressService.listAddress());
    }

    @PostMapping("/add")
    public Result<String> add(@RequestBody AddressVo addressVo) {
        addressService.addAddress(addressVo);
        return Result.success("添加成功");
    }
    @PutMapping("/update")
    public Result<String> update(@RequestBody AddressVo addressVo) {
        addressService.updateAddress(addressVo);
        return Result.success("更新成功");
    }

    @GetMapping("/detail/{id}")
    public Result<AddressVo> detail(@PathVariable Long id) {
        return Result.success(addressService.getAddressById(id));
    }

    @DeleteMapping("remove/{id}")
    public Result<String> remove(@PathVariable Long id) {
        addressService.removeById(id);
        return Result.success("删除成功");
    }

    @PutMapping("default/{id}")
    public Result<String> defaultAddr(@PathVariable Long id) {
        addressService.defaultAddr(id);
        return Result.success("设置成功");
    }

    @GetMapping("getAddress")
    public Result<AddressVo> getAddress() {
        return Result.success(addressService.getDefaultAddress());
    }
}
